package org.jeecg.modules.online.config.util;

import com.baomidou.mybatisplus.annotation.DbType;
import org.apache.commons.lang.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.DataBaseConstant;
import org.jeecg.common.util.dynamic.db.DbTypeUtils;

public class ColumnMeta {
	private String tableName;
	private String columnId;
	private  String columnName;
	private  int columnSize;
	private  String colunmType;
	private  String comment;//备注
	private  String fieldDefault;//默认值
	private  int decimalDigits;
	private  String isNullable; //Y 是 N 否
	private String pkType;//主键策略
	private String oldColumnName;//原来的字段名
	/**
	 * 数据库真实字段类型
	 */
	private String realDbType;


	@Override
	public boolean equals(Object obj) {
		 if (obj == this) {
	            return true;
	        }
	        if (!(obj instanceof ColumnMeta)) {
	            return false;
	        }
	        ColumnMeta meta = (ColumnMeta)obj;
	        //时间类型不比较长度
	          if (colunmType.contains("date") || colunmType.contains("blob") || colunmType.contains("text")) {
	        	  return columnName.equals(meta.getColumnName())
	  	        &&isNullable.equals(meta.isNullable)
	  	        &&isEquals(comment,meta.getComment())&&isEquals(fieldDefault,meta.getFieldDefault());
			}
	          /*else if (colunmType.contains("int")) {
				 return columnName.equals(meta.getColumnName())&& colunmType.equals(meta.getColunmType())
		        &&isNullable.equals(meta.isNullable);
			} */

	          else {
				 return colunmType.equals(meta.getColunmType())
			        &&isNullable.equals(meta.isNullable)&&columnSize==meta.getColumnSize()
			        &&isEquals(comment,meta.getComment())&&isEquals(fieldDefault,meta.getFieldDefault());
			}
	}

	//update-begin--Author:qinfeng  Date:20180118 for：Sqlserver2008 字段类型适配问题解决-------------------
	/**
	 * 新增对比方法： 针对Sqlserver2008数据库，不对比字段备注和默认值
	 *
	 * @param obj 对象
	 * @param dbType 数据库类型
	 * @return
	 */
	public boolean equalsByDataType(Object obj, DbType dbType) {
		 if (obj == this) {
	            return true;
	        }
	        if (!(obj instanceof ColumnMeta)) {
	            return false;
	        }
	        ColumnMeta meta = (ColumnMeta)obj;
	        //update-begin-author:taoyan date:20220118 for: JTC-315 【online表单】sqlserver 数据类型text 改为String 未生效
	        if(!colunmType.equals(meta.getColunmType())){
	        	return false;
			}
			//update-end-author:taoyan date:20220118 for: JTC-315 【online表单】sqlserver 数据类型text 改为String 未生效
	        if(DbTypeUtils.dbTypeIsSQLServer(dbType)){
	        	  //时间类型不比较长度
		        if (colunmType.contains("date") || colunmType.contains("blob") || colunmType.contains("text")) {
		        	  return columnName.equals(meta.getColumnName()) &&isNullable.equals(meta.isNullable);
				}else {
		        	// TODO author:taoyan date:20220105 for: JTC-141 JTC-319 判断字段是否修改 增加小数位判断判断
					 return colunmType.equals(meta.getColunmType()) && isNullable.equals(meta.isNullable)&&columnSize==meta.getColumnSize()&&decimalDigits==meta.getDecimalDigits()&&isEquals(fieldDefault,meta.getFieldDefault());
				}
				//update-begin--Author:scott----  Date:20190924 ----for：Postgresql数据库,Text类型不比较长度----
	        }else if(DbTypeUtils.dbTypeIsPostgre(dbType)){
			    //Text类型不比较长度
                if (colunmType.contains("date") || colunmType.contains("blob") || colunmType.contains("text")) {
                    return columnName.equals(meta.getColumnName()) &&isNullable.equals(meta.isNullable);
                }else {
					// TODO author:taoyan date:20220105 for:  JTC-141 JTC-319 判断字段是否修改 增加小数位判断判断
                    return colunmType.equals(meta.getColunmType()) && isNullable.equals(meta.isNullable)&&columnSize==meta.getColumnSize()&&decimalDigits==meta.getDecimalDigits()&&isEquals(fieldDefault,meta.getFieldDefault());
                }
				//update-begin--Author:scott----  Date:20190924 ----for：Postgresql数据库,Text类型不比较长度----
		    }else if(DbTypeUtils.dbTypeIsOracle(dbType)){
                //Text类型不比较长度
                if (colunmType.contains("date") || colunmType.contains("blob") || colunmType.contains("text")) {
                    //update-begin-author:taoyan date:20211217 for:原来是日期 现在改成字符串改不了
                	return colunmType.equals(meta.getColunmType()) && columnName.equals(meta.getColumnName()) &&isNullable.equals(meta.isNullable);
					//update-end-author:taoyan date:20211217 for:原来是日期 现在改成字符串改不了
                }else {
					// TODO author:taoyan date:20220105 for:  JTC-141 JTC-319 判断字段是否修改 增加小数位判断判断
                    return colunmType.equals(meta.getColunmType()) && isNullable.equals(meta.isNullable)&&columnSize==meta.getColumnSize()&&decimalDigits==meta.getDecimalDigits()&&isEquals(fieldDefault,meta.getFieldDefault());
                }
                //update-begin--Author:scott----  Date:20190924 ----for：Postgresql数据库,Text类型不比较长度----
            }else{
	            if (colunmType.contains("date") || colunmType.contains("blob") || colunmType.contains("text")) {
	            	//update-begin--Author:Yandong----  Date:20180521 ----for：TASK #2727 【online问题】UE编辑器图片 回显问题----blob类型报错 图片上传问题-----
	            	return colunmType.equals(meta.getColunmType()) && columnName.equals(meta.getColumnName())
		  	        &&isNullable.equals(meta.isNullable)
		  	        &&isEquals(comment,meta.getComment())&&isEquals(fieldDefault,meta.getFieldDefault());
	            	//update-end--Author:Yandong----  Date:20180521 ----for：TASK #2727 【online问题】UE编辑器图片 回显问题----blob类型报错 图片上传问题-----
				}else {
					// TODO author:taoyan date:20220105 for:  JTC-141 JTC-319 判断字段是否修改 增加小数位判断判断
					 return colunmType.equals(meta.getColunmType())
				        &&isNullable.equals(meta.isNullable)&&columnSize==meta.getColumnSize()&&decimalDigits==meta.getDecimalDigits()
				        &&isEquals(comment,meta.getComment())&&isEquals(fieldDefault,meta.getFieldDefault());
				}
	        }
	}
	//update-end--Author:qinfeng  Date:20180118 for：Sqlserver2008 字段类型适配问题解决-------------------

	public boolean equalsDefault(ColumnMeta meta){
		if (meta == this) {
			return true;
		}
		return isEquals(comment,meta.getComment());
	}

	public boolean equalsComment(ColumnMeta meta){
		if (meta == this) {
            return true;
        }
		return isEquals(comment,meta.getComment());
	}

	private boolean isEquals(String newS,String oldS){
		// update-begin-author:taoyan date:20210901 for: org/jeecg/modules/online/config/util/DbTableProcess.java:368 设置了默认值为""/null都是""
		boolean booN = StringUtil.isNotEmpty(newS);
		boolean booO = StringUtil.isNotEmpty(oldS);
		if("".equals(oldS)){
			// 这里有坑  数据库字段默认值为空 oldS一定是"" 但是newS可能传'null' 所以先检查 newS 是否为空
			if(!booN){
				return true;
			}
			// 再检查newS是否为null
			if(newS.toLowerCase().toString().indexOf("null")>=0){
				return true;
			}
			return false;
		}
		// update-end-author:taoyan date:20210901 for: org/jeecg/modules/online/config/util/DbTableProcess.java:368 设置了默认值为""/null都是""
		if(booN!=booO){return false;}
		if(booN){
			return newS.equals(oldS);
		}
		return true;
	}

	public String getColumnName() {
		return columnName;
	}
	public int getColumnSize() {
		return columnSize;
	}
	public String getColunmType() {
		return colunmType;
	}
	public String getComment() {
		return comment;
	}
	public int getDecimalDigits() {
		return decimalDigits;
	}
	public String getIsNullable() {
		return isNullable;
	}
	public String getOldColumnName() {
		return oldColumnName;
	}
    @Override
	public int hashCode() {
        return columnSize + colunmType.hashCode()*columnName.hashCode();
    }
	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}
	public void setColumnSize(int columnSize) {
		this.columnSize = columnSize;
	}

	 public void setColunmType(String colunmType) {
		this.colunmType = colunmType;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	public void setDecimalDigits(int decimalDigits) {
		this.decimalDigits = decimalDigits;
	}
	public void setIsNullable(String isNullable) {
		this.isNullable = isNullable;
	}
	public void setOldColumnName(String oldColumnName) {
		this.oldColumnName = oldColumnName;
	}

	@Override
	public String toString() {
		return columnName+","+colunmType+","+isNullable+","+columnSize;
	}
	public String getColumnId() {
		return columnId;
	}
	public void setColumnId(String columnId) {
		this.columnId = columnId;
	}
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public String getFieldDefault() {
		return fieldDefault;
	}
	public void setFieldDefault(String fieldDefault) {
		this.fieldDefault = fieldDefault;
	}

	public String getPkType() {
		return pkType;
	}

	public void setPkType(String pkType) {
		this.pkType = pkType;
	}

	public String getRealDbType() {
		return realDbType;
	}

	public void setRealDbType(String realDbType) {
		this.realDbType = realDbType;
	}
}